Skip to content

[13.x] Add Redis cache prefix clearing#60705

Draft
DGarbs51 wants to merge 1 commit into
13.xfrom
redis-prefix-cache-clear
Draft

[13.x] Add Redis cache prefix clearing#60705
DGarbs51 wants to merge 1 commit into
13.xfrom
redis-prefix-cache-clear

Conversation

@DGarbs51

@DGarbs51 DGarbs51 commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR adds an opt-in way for Redis cache stores to clear only keys managed by the configured Laravel cache prefix instead of flushing the selected Redis database.

It introduces:

  • Illuminate\Contracts\Cache\CanFlushPrefix
  • RedisStore::flushPrefix()
  • Repository::flushPrefix() / supportsFlushingPrefix()
  • php artisan cache:clear --prefix
  • cache.stores.redis.flush_scope, configurable with REDIS_CACHE_FLUSH_SCOPE=prefix

The existing Redis cache flush() behavior is unchanged and continues to call FLUSHDB unless the new prefix mode is explicitly requested.

Why

Laravel's default Redis configuration has historically encouraged isolating cache data in a separate logical Redis database. That is increasingly difficult with managed Redis-compatible services:

That means applications running on modern Redis Cluster, Redis Cloud, Redis Software, or ElastiCache Serverless deployments need a first-party way to clear only Laravel cache keys without clearing unrelated data that shares database zero.

Backwards Compatibility

This is intended to be backwards compatible:

  • RedisStore::flush() still calls flushdb().
  • cache:clear still uses the existing flush behavior by default.
  • Prefix clearing is only used when cache:clear --prefix is passed or when cache.stores.redis.flush_scope is set to prefix.
  • The new cache store capability is an optional contract, so existing cache store implementations are not required to add new methods.
  • Prefix clearing refuses to run when the Redis cache store has an empty prefix.

Tests

  • vendor/bin/pint --dirty
  • git diff --check
  • vendor/bin/phpunit tests/Cache/CacheRedisStoreTest.php tests/Cache/ClearCommandTest.php tests/Cache/CacheEventsTest.php
  • vendor/bin/phpunit tests/Cache

The full cache suite passes. PHPUnit reports the existing notices/skips from the suite: 8 notices and 13 skipped tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant